package order

import (
	"context"
	"time"

	"laundry-service/app/client/internal/svc"
	"laundry-service/app/client/internal/types"
	"laundry-service/entity"

	"github.com/zeromicro/go-zero/core/logx"
)

type ReservationOrderLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewReservationOrderLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ReservationOrderLogic {
	return &ReservationOrderLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *ReservationOrderLogic) ReservationOrder(req *types.ReservationrOrderReq) (resp *types.Reply, err error) {
	// 检查订单是否存在
	_, err = l.svcCtx.Db.FindOrderById(int(req.OrderId))
	if err != nil {
		return nil, err
	}

	// 保存预约订单
	ReservationOrder := new(entity.ReservationOrder)
	ReservationOrder.OrderId = req.OrderId
	ReservationOrder.PickupAddrId = req.PickupAddrId
	ReservationOrder.DeliveryAddrId = req.DeliveryAddrId
	ReservationOrder.AppointmentTime = req.AppointmentTime
	ReservationOrder.CreatedAt = time.Now().Format("2006-01-02 15:04:05")

	if err := l.svcCtx.Db.SaveReservationOrder(ReservationOrder); err != nil {
		return nil, err
	}

	return nil, nil
}
